package com.zzh.takeoutapp.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.adapter.EvaluateAdapter;
import com.zzh.takeoutapp.base.BaseFragment;
import com.zzh.takeoutapp.event.NetMsgEvent;
import com.zzh.takeoutapp.model.EvaluateBean;
import com.zzh.takeoutapp.service.EventbusHelper;
import com.zzh.takeoutapp.service.ServiceUrl;
import com.zzh.takeoutapp.utils.GsonResolve;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/7/27.
 */

public class EvaluateFragment extends BaseFragment {

    @Bind(R.id.evaluate_recyclerView)
    RecyclerView mRecyclerView;
    private EvaluateAdapter evaluateAdapter;
    private final String EVALUATE = "EvaluateFragment";

    @Override
    public void getsavedInstanceState(Bundle savedInstanceState) {

    }

    public static EvaluateFragment newInstance() {
        EvaluateFragment evaluateFragment = new EvaluateFragment();
        return evaluateFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_evaluate, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void initComponent(View view) {
    }

    @Override
    public void setListener() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));//很重要，不然不显示就是一个坑
    }

    @Override
    public void onAfterInit() {

    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onStart() {
        super.onStart();
        EventbusHelper.register(this);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventbusHelper.unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsgEvent(NetMsgEvent event) {
        String _msgType = (String) event.getMsgType();
        JSONObject jsonObject = event.getMsgJSONObj();
        switch (_msgType) {
            case EVALUATE:
                if (jsonObject.optString("error").equals("0")) {
                    EvaluateBean evaluateBean = GsonResolve.jsonString2Bean(jsonObject.toString(), EvaluateBean.class);
                    evaluateAdapter = new EvaluateAdapter(evaluateBean, getActivity());
                    mRecyclerView.setAdapter(evaluateAdapter);
                    evaluateAdapter.notifyDataSetChanged();
                }
                break;
        }
    }
}
